//
//  CollectionViewController.m
//  Food
//
//  Created by dlios on 15/7/21.
//  Copyright (c) 2015年 李志远. All rights reserved.
//

#import "CollectionViewController.h"
#import "DataBaseHandler.h"
#import "CollectionTableViewCell.h"
#import "MakeDetailObject.h"
#import "DrawerModel.h"
#import "BaseHeader.h"
#import "CollectionDetailViewController.h"
@interface CollectionViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *arrContent;

@property (nonatomic, retain) UITableView *myTableView;

@end

@implementation CollectionViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arrContent = [NSMutableArray array];
        self.arrInfo = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    self.title = @"我的收藏";
    DataBaseHandler *base = [DataBaseHandler shareInstance];
    self.arrContent = [base selectAll];
    if (self.arrContent.count == 0) {
        UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"提示" message:@"暂无收藏" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil] autorelease];
        [alertView show];
    }
    [self createView];
}

- (void)createView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView release];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.arrContent != 0) {

        CollectionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (!cell) {
            cell = [[[CollectionTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"] autorelease];
        }
        cell.model = self.arrContent[indexPath.row];
        return cell;
    } else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell1"];
        }
        cell.textLabel.text = @"暂无收藏";
        return cell;
    }
}
#pragma mark - 设置编辑方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
}

#pragma mark - tableview删除样式设置
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    CollectionDetailViewController *collection = [[[CollectionDetailViewController alloc] init] autorelease];
    collection.Code = [self.arrContent[indexPath.row] codeId];
    [self.navigationController pushViewController:collection animated:YES];
}
#pragma mark - tableView删除文字设置
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"立即删除";
}
#pragma mark - 编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

#pragma mark - 删除操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    DataBaseHandler *dbManager = [DataBaseHandler shareInstance];
    [dbManager openBD];
    [dbManager deleteFavoritesWithCodeId:[self.arrContent[indexPath.row] codeId]];
    [self.arrContent removeObjectAtIndex:indexPath.row];
    NSArray *indexArr = @[indexPath];
    [self.tableView deleteRowsAtIndexPaths:indexArr withRowAnimation:UITableViewRowAnimationTop];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrContent.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
 #pragma mark - Navigation

 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */


@end
